package studiodiva.portfolio.client.lib.view.gravatar;

import com.google.gwt.user.client.ui.Image;

import static studiodiva.portfolio.client.lib.lang.Lang.is;

import studiodiva.portfolio.client.lib.security.MD5;
import studiodiva.portfolio.client.lib.view.View;

public class GravatarImage extends View<Image> {

  private static final String GRAVATAR_URL = "http://www.gravatar.com/avatar/";
  private String email;
  private int size;

  public GravatarImage() {
    initWidget(new Image());
  }

  public void setEmail(String email) {
    this.email = email;
    updateUrl();
  }

  public void setSize(int size) {
    this.size = size;
    updateUrl();
  }

  private void updateUrl() {
    String url = GRAVATAR_URL + MD5.encode(email.trim().toLowerCase());
    if (is(size)) url += "?s=" + size;
    widget().setUrl(url);
  }
}
